package com.tedu.project5.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tedu.project5.common.Result;
import com.tedu.project5.dto.CategoryDTO;
import com.tedu.project5.dto.QueryCategorysDTO;
import com.tedu.project5.model.DdCategorysInfo;
import com.tedu.project5.mapper.DdCategorysInfoMapper;
import com.tedu.project5.service.IDdCategorysInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 产品类型表 服务实现类
 * </p>
 *
 * @author mhg
 * @since 2022-06-20
 */
@Service
public class DdCategorysInfoServiceImpl extends ServiceImpl<DdCategorysInfoMapper, DdCategorysInfo> implements IDdCategorysInfoService {

    /**
     * 依赖持久层
     */
    @Autowired(required = false)
    private DdCategorysInfoMapper ddCategorysInfoMapper;

    /**
     * 查询分类列表
     * @return
     */
    @Override
    public Result queryExample(QueryCategorysDTO dto) {
        QueryWrapper<DdCategorysInfo> wrapper = new QueryWrapper<>();
        //查询条件
        if (dto.getCategoryName()!=null){
            //第一个参数是表里的字段名，第二个参数是前端传过来的值
            wrapper.like("category_name",dto.getCategoryName());
        }
        List<DdCategorysInfo> ddCategorysInfos = ddCategorysInfoMapper.selectList(wrapper);
        return Result.ok(ddCategorysInfos);
    }

    /**
     * 新增分类
     * @param category
     * @return
     */
    @Override
    public Result saveCategory(DdCategorysInfo category) {
        if (category.getCategoryName()!=null ){
            LocalDateTime now = LocalDateTime.now();
            category.setCreateTime(now);
            category.setUpdateTime(now);
            int insert = ddCategorysInfoMapper.insert(category);
            if (insert>=0){
                return Result.ok();
            }else{
                return Result.error("插入失败");
            }
        }else{
            return Result.error("参数缺失");
        }
    }
    /**
     * 更新分类信息
     * @param dto
     * @return
     */
    @Override
    public Result updateCategory(CategoryDTO dto) {
        //校验数据合法性
        if (dto!=null && dto.getId()!=null && dto.getCategoryName()!=null){
            //组装数据
            DdCategorysInfo pojo = new DdCategorysInfo();
            pojo.setId(dto.getId());
            pojo.setCategoryName(dto.getCategoryName());
            //保存到数据库
            int i = ddCategorysInfoMapper.updateById(pojo);
            if (i!=0){
                return Result.ok();
            }else{
                return Result.error("更新失败");
            }
        }else{
            return Result.error("数据不合法！");
        }
    }
    /**
     * 删除分类信息
     * @param id
     * @return
     */
    @Override
    public Result deleteCategory(String id) {
        int i = ddCategorysInfoMapper.deleteById(id);
        if (i>=0){
            return Result.ok();
        }else{
            return Result.error("删除失败");
        }
    }
}
